package com.lg.algorithm.base;

import com.lg.algorithm.collections.list.my.SingleList;
import com.lg.algorithm.collections.list.my.SingleNode;
import lombok.Data;

import java.util.Arrays;

/**
 * 将链表 方向重置 并打印
 * <p>
 * a > b > c > d
 * b > a > d > c
 * <p>
 * a > b > c > d > e
 * b > a > d > c > e
 */
public class ListReSort {

    @Data
    static
    class Parameters {
        int index = 1;
        SingleNode head;
        SingleNode grandFather;
        SingleNode current;

        Parameters(SingleNode head) {
            this.head = head;
            this.current = head;
        }

    }


    private static void resort(Parameters parameters) {
        if (parameters.getCurrent() == null) {
            return;
        }

        SingleNode grandFather = parameters.getGrandFather();
        SingleNode parent = null;
        if (grandFather != null) {
            parent = grandFather.getNext();
        }
        SingleNode current = parameters.getCurrent();
        SingleNode next =   current.getNext();
        SingleNode head = parameters.getHead();
        int index = parameters.getIndex();

        if (index == 1) {
            //设置下行参数
            parameters.setGrandFather(grandFather);
            parameters.setCurrent(next);
            parameters.setIndex(++index);
            parameters.setHead(head);
        } else if (index == 2) {
            current.setNext(head);
            head.setNext(next);

            //设置下行参数
            parameters.setGrandFather(current);
            parameters.setHead(current);
            parameters.setCurrent(next);
            parameters.setIndex(++index);
        } else if (index > 3 && index % 2 == 0) {
            //转换
            grandFather.setNext(current);
            current.setNext(parent);
            parent.setNext(next);

            //设置下行参数
            parameters.setGrandFather(grandFather.getNext());
            parameters.setHead(head);
            parameters.setCurrent(next);
            parameters.setIndex(++index);
            resort(parameters);

        } else {
            //设置下行参数
            parameters.setGrandFather(grandFather.getNext());
            parameters.setHead(head);
            parameters.setCurrent(next);
            parameters.setIndex(++index);
        }

        resort(parameters);

    }

    private static void resort2(Parameters parameters) {
        if (parameters.getCurrent() == null) {
            return;
        }
        SingleNode grandFather = parameters.getGrandFather();
        SingleNode parent = grandFather == null ? null : grandFather.getNext();
        SingleNode current = parameters.getCurrent();
        SingleNode next =   current.getNext();
        SingleNode head = parameters.getHead();
        int index = parameters.getIndex();

        if (index == 2) {
            current.setNext(head);
            head.setNext(next);

            //设置下行参数
            parameters.setGrandFather(current);
            parameters.setHead(current);
            parameters.setCurrent(next);
            parameters.setIndex(++index);
            resort2(parameters);
            return;
        }

        if (index % 2 == 0) {
            //转换
            grandFather.setNext(current);
            current.setNext(parent);
            parent.setNext(next);
        }


        //设置下行参数
        //因为链表动了 索引不能 parent 要重新拿
        parameters.setGrandFather(grandFather == null ? null:grandFather.getNext());
        parameters.setHead(head);
        parameters.setCurrent(next);
        parameters.setIndex(++index);

        resort2(parameters);

    }

    public static void main(String[] args) {

        SingleNode head = SingleList.createSingleList(Arrays.asList(1, 2, 3, 4, 5, 6));
        SingleList.printList(head);

        System.out.println("-----------------");

        Parameters parameters = new Parameters(head);
        resort2(parameters);
        SingleList.printList(parameters.getHead());
    }
}
